package com.hrbxlh.virtualization.result;

import org.apache.commons.lang3.builder.ToStringBuilder;

/**
 * @author cheer
 */
public enum ResultEnum {

    SUCCESS(200, "请求成功"),

    NOT_FOUND(404, "内容不存在"),

    // 通用异常
    SERVER_ERROR(500100, "服务端异常"),
    BIND_ERROR(500101, "", "%s"),
    DOCKER_ERROR(500102, "虚拟机环境异常"),
    // 容器异常
    CONTAINERS_DELETE_ERROR(500200, "", "虚拟机删除异常，异常虚拟机如下：%s"),
    CONTAINER_NOT_EXISTS(500201, "虚拟机不存在"),
    CONTAINER_CREATE_ERROR(500202, "虚拟机创建异常"),
    CONTAINER_QUERY_ERROR(500203, "虚拟机查询异常"),
    CONTAINER_DELETE_ERROR(500204, "虚拟机删除异常"),
    CONTAINER_START_ERROR(500205, "虚拟机启动异常"),
    CONTAINER_ERROR(500206, "虚拟机故障，请替换虚拟机"),
    CONTAINER_STOP_ERROR(500207, "虚拟机停止异常"),
    CONTAINER_CMD_ERROR(500208, "虚拟机执行命令异常"),
    // 集群异常
    CLUSTER_NAME_EXISTS(500300, "集群名称已存在"),
    CLUSTER_MASTER_IMAGE_NOT_EXISTS(500301, "集群主节点选择的镜像不存在"),
    CLUSTER_FIRST_SLAVE_IMAGE_NOT_EXISTS(500302, "集群第一个从节点选择的镜像不存在"),
    CLUSTER_SECOND_SLAVE_IMAGE_NOT_EXISTS(500303, "集群第二个从节点选择的镜像不存在"),
    CLUSTER_NOT_EXISTS(500304, "集群不存在"),
    CLUSTER_IS_USED(500305, "该集群正在使用，删除该集群会导致在‘镜像分配’功能中已分配的课程章节中的集群也被删除，请确认是否删除？"),
    CLUSTER_BROKEN(500306, "集群已经损坏，集群包含已被删除的镜像"),
    CLUSTER_DELETE_INITIAL_NOT_ALLOWED(500307, "不允许删除预置集群"),
    CLUSTER_DELETE_NOT_ALLOWED(500308, "不允许删除别人的集群"),
    // 镜像异常
    IMAGE_DELETE_INITIAL_NOT_ALLOWED(500400, "不允许删除预置镜像"),
    IMAGE_IS_USED(500401, "该镜像正在使用，删除该镜像会导致在‘镜像分配’功能中已分配的课程章节中的镜像也被删除，请确认是否删除？"),
    IMAGE_DELETE_ERROR(500402, "", "镜像删除异常，异常原因：%s"),
    IMAGE_NOT_EXISTS(500403, "镜像不存在"),
    IMAGE_QUOTA_EXCEEDED(500404, "", "每个教师保存的镜像的数量不能超过 %s 个"),
    IMAGE_NAME_EXISTS(500405, "镜像名称已存在"),
    IMAGE_SHARE_NOT_ALLOWED(500406, "不允许共享别人的镜像"),
    IMAGE_UNSHARE_NOT_ALLOWED(500407, "不允许取消共享别人的镜像"),
    IMAGE_DELETE_NOT_ALLOWED(500408, "不允许删除别人的镜像"),
    IMAGE_COVER_SHARED_NOT_ALLOWED(500409, "不允许覆盖公有的镜像"),
    IMAGE_COVER_NOT_ALLOWED(500410, "不允许覆盖别人的镜像"),
    IMAGE_COMMIT_ERROR(500411, "镜像构建异常"),
    IMAGE_PUSH_ERROR(500412, "镜像上传异常"),
    IMAGE_REPOSITORY_ERROR(500413, "镜像仓库访问异常"),
    IMAGE_ALLOCATION_UNSHARE_NOT_ALLOWED(500414, "不允许取消共享已发布课程所配置的镜像"),
    // 镜像分配异常
    IMAGE_ALLOCATION_NOT_EXISTS(500500, "当前章节没有分配镜像，如果要使用虚拟机，请联系任课教师或管理员"),
    IMAGE_ALLOCATION_DELETED(500501, "当前章节分配的镜像已被删除，如果要使用虚拟机，请联系任课教师或管理员"),
    CLUSTER_ALLOCATION_DELETED(500502, "当前章节分配的集群已被删除，如果要使用虚拟机，请联系任课教师或管理员"),
    CLUSTER_ALLOCATION_BROKEN(500503, "当前章节分配的集群已经损坏，集群包含已被删除的镜像，如果要使用虚拟机，请联系任课教师或管理员"),
    IMAGE_ALLOCATION_CANCEL_NOT_EXISTS(500504, "选择的章节没有分配镜像"),
    // 桌面异常
    DESKTOP_BUSY(500600, "系统忙碌，请稍后再试"),
    CLIPBOARD_ERROR(500601, "剪切板复制失败"),
    UPLOAD_ERROR(500606, "上传文件失败"),
    DESKTOP_SHARE_NOT_ALLOWED(500602, "不允许分享别人的桌面"),
    DESKTOP_UNSHARE_NOT_ALLOWED(500603, "不允许取消分享别人的桌面"),
    DESKTOP_QUERY_ERROR(500604, "桌面查询异常"),
    FTP_ERROR(500605, "FTP地址查询异常"),
    // 报告异常
    CONTAINER_FILE_NOT_EXISTS(500700, "虚拟机文件不存在");

    private Integer code;

    private String message;

    private String template;

    public ResultEnum fillArgs(Object... args) {
        this.message = String.format(this.template, args);
        return this;
    }

    ResultEnum(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    ResultEnum(Integer code, String message, String template) {
        this.code = code;
        this.message = message;
        this.template = template;
    }

    public Integer getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public String getTemplate() {
        return template;
    }

    @Override
    public String toString() {
        return new ToStringBuilder(this)
                .append("code", code)
                .append("message", message)
                .append("template", template)
                .toString();
    }
}
